package com.mxdx.edu.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.mxdx.edu.common.ApiResult;
import com.mxdx.edu.common.CommonUtils;
import com.mxdx.edu.common.log.AppLogger;
import com.mxdx.edu.enums.ErrorEnums;
import com.mxdx.edu.service.ICourseService;
import com.mxdx.edu.service.ICourseTutorshipService;
import com.mxdx.edu.service.ICourseVideoService;
import com.mxdx.edu.service.IMajorCourseService;
import com.mxdx.edu.service.IMajorService;
import com.mxdx.edu.service.ITutorshipService;
import com.mxdx.edu.service.IVideoService;
import com.mxdx.edu.util.ServletUtil;

@Controller
@RequestMapping(value = "/curriculum")
public class CurriculumController extends BaseController{

	private AppLogger logger = new AppLogger(CurriculumController.class);

	@Resource
	private IMajorService majorService;

	@Resource
	private ICourseService courseService;

	@Resource
	private IMajorCourseService majorCourseService;

	@Resource
	private ICourseTutorshipService courseTutorshipService;

	@Resource
	private ICourseVideoService courseVideoService;

	@Resource
	private ITutorshipService tutorshipService;

	@Resource
	private IVideoService videoService;


	/**
	 * 跳转课程分类信息
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/auth/toCurriculum")
	public ModelAndView toCurriculum(HttpServletRequest request){
		logger.entry(ServletUtil.getClientIp(request), "toCurriculum");
		ModelAndView result = new ModelAndView("/h5/curriculum");
		ApiResult<String> apiResult = new ApiResult<>();
		List<Map<String, Object>> majorList = null;
		List<Map<String, Object>> childrenMajorList = new ArrayList<>();
		String parentId = ServletUtil.getParameterStr(request, "parentId", "10");
		Map<String, Object> param = new HashMap<>();
		String token = sessionUtil.getTokenSession(request);
		Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
		String studentType = CommonUtils.getStrValueFromMap(map, "studentType", "");
		String headPortrait = CommonUtils.getStrValueFromMap(map, "headPortrait", "");
		String studentName = CommonUtils.getStrValueFromMap(map, "studentName", "");
		String mood = CommonUtils.getStrValueFromMap(map, "mood", "");
		//先默认取学生类型为1的
		Integer classificationId = Integer.valueOf(parentId);
		if(Integer.valueOf(studentType) == 3){
			classificationId+=1;
		}else{
			classificationId+= Integer.valueOf(studentType);
		}
		param.put("parentId", classificationId);
		try{
			majorList = majorService.findListByMap(param);
			Map<String, Object> childrenMap = null;
			for(Map<String, Object> majorMap : majorList){
				childrenMap = new HashMap<>();
				String id = CommonUtils.getStrValueFromMap(majorMap, "id", "");
				param.put("parentId", id);
				List<Map<String, Object>> childrenMajor = majorService.findListByMap(param);
				childrenMap.put(id, childrenMajor);
				childrenMajorList.add(childrenMap);
			}
			logger.info(ServletUtil.getClientIp(request));
		}catch (Exception e){
			dealException(ErrorEnums.SYSTEM_ERROR, e, apiResult, logger);
		}
		result.addObject("majorList", JSONObject.toJSONString(majorList));
		result.addObject("childrenMajorList", JSONObject.toJSONString(childrenMajorList));
		result.addObject("studentInfo", JSONObject.toJSONString(map));
		result.addObject("headPortrait", headPortrait);
		result.addObject("studentName", studentName);
		result.addObject("mood", mood);
		return result;
	}

	/**
	 * 跳转二级分类信息
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/auth/toChildrenCurriculum")
	public ModelAndView toChilenCurriculum(HttpServletRequest request){
		logger.entry(ServletUtil.getClientIp(request), "toChildrenCurriculum");
		ModelAndView result = new ModelAndView("/h5/childrenCurriculum");
		ApiResult<String> apiResult = new ApiResult<>();
		String parentId = ServletUtil.getParameterStr(request, "parentId", "");
		String majorName = ServletUtil.getParameterStr(request, "majorName", "");
		String parentName = ServletUtil.getParameterStr(request, "parentName", "");
//		List<Map<String, Object>> childrenCurriculum = new ArrayList<>();
		List<Map<String, Object>> majorCourseList = new ArrayList<>();
		Map<String, Object> param = new HashMap<>();
		String headPortrait = "";
		String studentName = "";
		String mood = "";
		try {
			String token = sessionUtil.getTokenSession(request);
			Map<String, Object> studentMap = (Map<String, Object>)redisUtil.get(token);
			headPortrait = CommonUtils.getStrValueFromMap(studentMap, "headPortrait", "");
			studentName = CommonUtils.getStrValueFromMap(studentMap, "studentName", "");
			mood = CommonUtils.getStrValueFromMap(studentMap, "mood", "");
//			String studentType = CommonUtils.getStrValueFromMap(studentMap, "studentType", "");
			param.put("majorId", parentId);
			majorCourseList = majorCourseService.findListByMap(param);
//			for(Map<String, Object> map : majorCourseList){
//				String courseName = CommonUtils.getStrValueFromMap(map, "courseShowName", "");
//				map.put("courseName", courseName);
//				childrenCurriculum.add(map);
//			}
			logger.info(ServletUtil.getClientIp(request));
		}catch (Exception e){
			dealException(ErrorEnums.SYSTEM_ERROR, e, apiResult, logger);
		}
		result.addObject("headName", String.format("%s-%s", parentName, majorName));
		result.addObject("childrenCurriculum", JSONObject.toJSONString(majorCourseList));
		result.addObject("headPortrait", headPortrait);
		result.addObject("studentName", studentName);
		result.addObject("mood", mood);
		return result;
	}

	/**
	 * 跳转课程列表
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/auth/curriculumList")
	public ModelAndView curriculumList(HttpServletRequest request){
		logger.entry(ServletUtil.getClientIp(request), "curriculumList");
		ApiResult<String> apiResult = new ApiResult<>();
		List<Map<String, Object>> courseList = new ArrayList<>();
		ModelAndView result = new ModelAndView("/h5/curriculumList");
		List<Map<String, Object>> tutorshipList = new ArrayList<>();
		List<Map<String, Object>> videoList = new ArrayList<>();
		String courseId = ServletUtil.getParameterStr(request, "courseId", "");
		String courseName = ServletUtil.getParameterStr(request, "courseName", "");
		String token = sessionUtil.getTokenSession(request);
		Map<String, Object> studentMap = (Map<String, Object>)redisUtil.get(token);
		String headPortrait = CommonUtils.getStrValueFromMap(studentMap, "headPortrait", "");
		String studentName = CommonUtils.getStrValueFromMap(studentMap, "studentName", "");
		String studentType = CommonUtils.getStrValueFromMap(studentMap, "studentType", "");
		String mood = CommonUtils.getStrValueFromMap(studentMap, "mood", "");
		try{
			Map<String, Object> param = new HashMap<>();
			param.put("courseId", courseId);
			//辅导资料
			tutorshipList = courseTutorshipService.findListByMap(param);
			//课程
			videoList = courseVideoService.findListByMap(param);
			for(Map<String, Object> videoMap : videoList){
				String videoName = CommonUtils.getStrValueFromMap(videoMap, "videoName", "");
				String name = videoName.substring(0, videoName.lastIndexOf("."));
				videoMap.put("videoName", name);
			}
			logger.info(ServletUtil.getClientIp(request));
		}catch (Exception e){
			dealException(ErrorEnums.SYSTEM_ERROR, e, apiResult, logger);
		}
		result.addObject("courseList", JSONObject.toJSONString(courseList));
		result.addObject("tutorshipList", tutorshipList);
		result.addObject("videoList", videoList);
		result.addObject("courseName", courseName);
		result.addObject("courseId", courseId);
		result.addObject("headPortrait", headPortrait);
		result.addObject("studentName", studentName);
		result.addObject("studentType", studentType);
		result.addObject("mood", mood);
		return result;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/auth/courseOnLine")
	public ModelAndView courseOnLine(HttpServletRequest request){
		logger.entry(ServletUtil.getClientIp(request), "courseOnLine");
		ApiResult<Map<String, Object>> apiResult = new ApiResult<>();
		ModelAndView result = new ModelAndView("/h5/ketangzaixian");
		List<Map<String, Object>> semesteList = new ArrayList<>();
		String studentName = "";
		String headPortrait = "";
		String mood = "";
		//String semesterName = "";
		//String semesterId = "";
		try{
			String token = sessionUtil.getTokenSession(request);
			Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
			String studentId = CommonUtils.getStrValueFromMap(map, "studentId", "");
			headPortrait = CommonUtils.getStrValueFromMap(map, "headPortrait", "");
			studentName = CommonUtils.getStrValueFromMap(map, "studentName", "");
			mood = CommonUtils.getStrValueFromMap(map, "mood", "");
			//通过学生id查询学生信息
			Map<String, Object> findStudentMap = new HashMap<>();
			findStudentMap.put("studentId", studentId);
			Map<String, Object> studentMap = studentService.findStudentMapOneByMap(findStudentMap);
			String classId = CommonUtils.getStrValueFromMap(studentMap, "classId", "");
			semesteList = semesterService.findListByClassId(classId);
			//semesterName = CommonUtils.getStrValueFromMap(semesteList.get(0), "semesterName", "");
			//semesterId = CommonUtils.getStrValueFromMap(semesteList.get(0), "id", "");
			logger.info(ServletUtil.getClientIp(request));
		}catch (Exception e){
			super.dealException(ErrorEnums.SYSTEM_ERROR, e, apiResult, logger);
		}
		result.addObject("courseList", semesteList);
		result.addObject("studentName", studentName);
		result.addObject("headPortrait", headPortrait);
		result.addObject("mood", mood);
		//result.addObject("semesterName", semesterName);
		//result.addObject("semesterId", semesterId);

		return result;
	}

}
